/**
 * 终端列表
 * */ 

Ext.define('MobileTest.view.mobile.MobilePanel', {
			extend:'Ext.panel.Panel',				
			ids:null,
			viewConfig : {
				stripeRows : true
			},
			curNodeId:0,			
			initComponent : function() {
				
			var me = this;
										
			var tvstore = Ext.create('Ext.data.TreeStore', {
							autoLoad:true,
							proxy: {
            					type: 'ajax',
            					url: 'DeviceTree'            					
        					},
        					 root: {
            					text: '.',
            					id: '0',     
            					expanded: true
        					},
        					 rootVisible: true
						});
		
	var gridstore=	Ext.create('Ext.data.Store', {    	
    			fields:[    			
    			'devId', 'devType', 'devName', 'testMode', 'provCode', 'cityCode', 'msin', 'ip', 'swversion', 'venderCode', 
    			'status',{name: 'createdt',type:'date'},{name: 'updatedt',type:'date'},{name:'nodeId',type:'int'}
    			],    	
    			proxy: {
        			type: 'ajax',
        			url : 'Devices',
        			reader: {
            			type: 'json',
            			root: 'rows'
        			}        	
    			}
			});
				
		
		Ext.applyIf(me, {
			layout:'border',  
			items:[			
				{
					xtype:'treepanel',
					region:'west',
					title: '终端组',
    				width: 200,
    				store: tvstore,
    				dockedItems : [ {
						xtype : 'toolbar',
						dock : 'top',
						items : [ {
							text : '添加',
							tooltip : '添加分组',
							iconCls : 'add',
							handler : function() {
								
								Ext.MessageBox.prompt('添加', '请输入组名:   ', function(btn, text){
                        								 
                        								 if(btn=='cancel')return;
                        								 
                        								 var title=Ext.String.trim(text);
                        								 if(title!=''){
                        								
                        										Ext.Ajax.request({
                        											url : 'newDeviceNode',
                        											params : {
                        												newNodeName :title,
                        												nodeId:1
                        											},
                        											success : function(response) {
                        												var text = response.responseText;

                        												var m = Ext.JSON.decode(text);
                        												
                        												alert(m.msg);
                        												
                        												if(m.success){										
                        													tvstore.load();
                        												}
                        									
                        											}});
                        									 
                        									 
                        								 }else{
                        									 alert("组名不能为空！");	 
                        								 }								 
                        								 
                        							 });
							
								}
							},{
							text : '删除',
							tooltip : '删除分组',						
							iconCls : 'del',
							handler : function() {
								
							}
						}]}
					],
    				rootVisible: false,
    				viewConfig:{
    					 listeners: {
                            render: function (tree) {
                                 new Ext.dd.DropTarget(tree.el, {
                                    ddGroup: 'gridtotree',
                                    copy: false,
									notifyEnter: function(ddSource, e, data) {
										
										var treePanel=me.child('treepanel');
            							treePanel.body.stopAnimation();
            							treePanel.body.highlight();
        							},
                                    notifyDrop: function (dragSource, event, data) {
                                    	
                                    	if(data.records.length<=0) return;
                                    	                                    	
                                        var devId = data.records[0].data.devId;
                                        
                                        //alert(devId);
                                        
                                        if(me.curNodeId>0){
                                        	
                                        	//save
                                        	Ext.Ajax.request({
													url : 'updateDeviceGroup',
													params : {
														nodeId : me.curNodeId,
														devId:devId
												},
												success : function(response) {
													var text = response.responseText;

													var m = Ext.JSON.decode(text);
												
													if(m.success){										
														gridstore.load({params:{key:''}});
													}else{
														alert(m.msg);
													}
													
												}});
                                        	
                                        }
                                    }
                                });
                        },
                        itemmouseenter:function(view, model, htmlItem, index, e){
                        	
                        	me.curNodeId=model.data.id;
                        	
                        }
                        }
    				}
				},{
					xtype:'gridpanel',
					region:'center',
					title: '终端列表',
    				store: gridstore,
    				viewConfig: {
            			plugins: {
                			ddGroup: 'gridtotree',
                			ptype: 'gridviewdragdrop',
                			enableDrop: false
            			}
        			},
    				enableDragDrop   : true,
    				columns: [
        				{ header: '设备类型',  dataIndex: 'devType' },
        				{ header: '设备名称', dataIndex: 'devName' },
        				{ header: '厂商编码', dataIndex: 'venderCode'},
        				{ header: '号码', dataIndex: 'msin' },
        				{ header: '状态', dataIndex: 'status' },
        				{ header: '省', dataIndex: 'provCode' },
        				{ header: '市', dataIndex: 'cityCode' },
        				{ header: '所属组', dataIndex: 'nodeId' }
    				],
    				dockedItems : [ {
					xtype : 'toolbar',
					dock : 'top',
					items : [/** {
						text : '添加',
						tooltip : '添加测试任务',
						iconCls : 'add',
						handler : function() {

							Ext.create('MobileTest.view.window.TestCaseWindow').show();
							
							}
						},{
							text : '删除',
							tooltip : '删除测试任务',						
							iconCls : 'del',
							handler : function() {
								
							}
						},**/'-',{	
							xtype    : 'textfield',
				            name     : 'txt_search_key',
				            emptyText: '请输入要查询号码'
				        },{				          
				            text: '检索',
				            tooltip : '检索查询号码',						
							iconCls : 'search',
				            handler : function() {
								
				            	var key=this.up('toolbar').child('textfield').getValue();
				            	
				               	me.store.load({params:{mobile:key}});
				            }
				        } ]}
				        ,{
			        		xtype: 'pagingtoolbar',
			        		store: gridstore,   
			        		dock: 'bottom',
			        		displayInfo: true
			    		}
				    ]
				}
			]
		});
				me.callParent(arguments);
				
				gridstore.load({params:{key:''}});
					
				//me.on('afterrender',me.on_afterrender);			
			},
			on_afterrender:function( me,  eOpts){
				
				var treePanel=me.child('treepanel');

    var treePanelDropTargetEl = me.child('treepanel').body.dom; //formPanel.body.dom;

     Ext.create('Ext.dd.DropTarget', treePanelDropTargetEl, {
        ddGroup: 'GridExample',
        notifyEnter: function(ddSource, e, data) {
			
            treePanel.body.stopAnimation();
            treePanel.body.highlight();
        },
        notifyDrop  : function(ddSource, e, data){
			        
            var selectedRecord = ddSource.dragData.records[0];
        
            treePanel.getLoader().load();

            return true;
        }
    });	
			}
			
			
					
		});